﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BizTalkFactory.Adapters.AzureStorage
{
    public sealed class NodeIdHelper
    {
        public static string ParseNodePrefixFromOperationId(string operationId)
        {
            int index = -1;

            if ((index = operationId.IndexOf("/")) != -1)
                return operationId.Substring(0, index + 1);

            return String.Empty;
        }

        public static string ParseOperationFromOperationId(string operationId)
        {
            string[] strings = operationId.Split(new char[] { '/' });

            if (strings.Length == 3)
                return strings[2];

            return string.Empty;
        }

        public static string ParseObjectNameFromOperationId(string operationId, ref string prefix, ref string operation)
        {
            prefix = ParseNodePrefixFromOperationId(operationId);

            string[] strings = operationId.Split(new char[] { '/' });

            if (strings.Length == 3)
            {
                operation = strings[2];
                return strings[1];
            }
            else if (strings.Length == 2)
            {
                operation = String.Empty;
                return strings[1];
            }

            return string.Empty;
        }

        public static string ParseObjectNameFromOperationId(string operationId)
        {
            string storage = String.Empty;
            string operation = String.Empty;

            return ParseObjectNameFromOperationId(operationId, ref storage, ref operationId);
        }

        public static string CreateOperationId(string prefix, string name, string operation)
        {
            return
            String.Format("{0}{1}/{2}"
                    , prefix.EndsWith("/") ? prefix : prefix + "/"
                    , name
                    , operation)
                    ;
        }
    }
}
